package com.aaa.controller;

import com.aaa.entity.Category;
import com.aaa.entity.Goods;
import com.aaa.service.ICategoryService;
import com.aaa.service.IGoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("/admin/goods")
@Controller
public class AdminGoodsController {
    @Resource
    private ICategoryService categoryService;
    @Resource
    private IGoodsService goodsService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<Goods> goodsList = goodsService.list();
        model.addAttribute("goodsList",goodsList);
        return "/admin/goods_list";
    }

    @RequestMapping("/add_show")
    public String add_show(Model model) {
        List<Category> categoryList = categoryService.list();
        model.addAttribute("categoryList",categoryList);
        return "/admin/goods_add";
    }

    @RequestMapping("/add_submit")
    public String add_submit(Goods goods, MultipartFile file) {
        String imgFile = null;
        //判断是否上传了图片
        if (file.isEmpty()){
            //如果为空，使用默认图片
            imgFile="/img/mr.png";
        }else {
            String newFile = UUID.randomUUID()+"-"+file.getOriginalFilename();
            File f = new File("G:/photo",newFile);
            //判断上级目录是否存在，不存在就创建
            if (!f.getParentFile().exists()){
                f.mkdirs();
            }
            try {
                //将文件存储到指定磁盘位置
                file.transferTo(f);
                //记录上传的文件的web访问路径
                imgFile="/upfile/"+newFile;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //设置goods对象的图片路径
        goods.setPicture(imgFile);
        //保存goods对象到数据库
        goodsService.insert(goods);
        return "redirect:/admin/goods/list";
    }

    @RequestMapping("/update_show")
    public String update_show(Integer goodsId,Model model){
        //根据编号获取商品信息
        Goods goods = goodsService.find(goodsId);
        List<Category> categoryList = categoryService.list();
        model.addAttribute("goods",goods);
        model.addAttribute("categoryList",categoryList);
        return "/admin/goods_update";
    }

    @RequestMapping("/update_submit")
    public String update_submit(Goods goods, MultipartFile file) {
        String imgFile = null;
        //判断是否上传了图片
        if (!file.isEmpty()){
            String newFile = UUID.randomUUID()+"-"+file.getOriginalFilename();
            File f = new File("G:/photo",newFile);
            //判断上级目录是否存在，不存在就创建
            if (!f.getParentFile().exists()){
                f.mkdirs();
            }
            try {
                //将文件存储到指定磁盘位置
                file.transferTo(f);
                goods.setPicture("/upfile/"+newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //保存goods对象到数据库
        goodsService.update(goods);
        return "redirect:/admin/goods/list";
    }
}
